package com.github.easyms.server.config.runner;

import com.github.easyms.common.entity.auth.Menu;
import com.github.easyms.common.entity.auth.Role;
import com.github.easyms.common.entity.auth.User;
import com.github.easyms.common.property.EasymsProperties;
import com.github.easyms.common.util.EncryptionHelper;
import com.github.easyms.server.service.auth.MenuService;
import com.github.easyms.server.service.auth.RoleService;
import com.github.easyms.server.service.auth.UserService;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * 启动时检查默认数据(默认账号、默认角色、默认菜单)
 *
 * @author Leo.Liao
 * @date 2020-05-02 02:31:59
 */
@Component
@Order(value = 1)
public class CheckDefaultData implements ApplicationRunner {

    @Resource
    private EasymsProperties easymsProperties;

    @Resource
    private RoleService roleService;

    @Resource
    private MenuService menuService;

    @Resource
    private UserService userService;

    private User defaultUser;
    private Role defaultRole;
    private Menu defaultMenu;

    @Override
    public void run(ApplicationArguments args) throws Exception {

        //  TODO
        // 检查默认角色
//        Role role = roleService.getById(easymsProperties.getDefaultRole());
//        if(role == null){
//            role = new Role();
//            role.setRoleId(easymsProperties.getDefaultRole());
//            role.setRoleId("Sys Admin");
//            roleService.save(role);
//        }
//
//        Menu menu = menuService.getById(easymsProperties.getDefaultMenu());
//        if(menu == null){
//            menu = new Menu();
//            menu.setMenuId(easymsProperties.getDefaultMenu());
//            menu.setMenuType("1");
//            menu.setPMenuId("0");
//            menu.setSort(0);
//            menu.setTextEn("root");
//            menu.setTextZhCn("root");
//            menu.setTextZhHk("root");
//            menu.setTextZhTw("root");
//            menuService.save(menu);
//        }
//
//        User user = userService.getById(easymsProperties.getDefaultUser());
//        if(user == null){
//            user.setUserId(easymsProperties.getDefaultUser());
//            user.setAccount("sys");
//            user.setSalt(easymsProperties.getDefaultUser());
//            user.setPassword(EncryptionHelper.md5Encrypt(easymsProperties.getDefaultPwd(),easymsProperties.getDefaultUser()));
//            user.set
//        }


    }

}
